<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
		<title>calendar_test</title>
		<link rel="stylesheet" href="<?php echo BASE_URL(); ?>assets/css/reset.css" type="text/css" media="screen">
		<link rel="stylesheet" href="<?php echo BASE_URL(); ?>assets/css/master.css" type="text/css" media="screen">
		<script src="<?php echo BASE_URL(); ?>assets/js/mootools-release-1.11.js" type="text/javascript" charset="utf-8"></script>
		<script src="<?php echo BASE_URL(); ?>assets/js/nerdycalendar-0.5.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" charset="utf-8">
			var myDayArray = new Array();

			<?php
			foreach ($result->result() as $row)
			{
				echo "var entry = new Object();";
				echo "entry.id = " . $row->id . "; ";
				echo "entry.start = " . ( date( "G", $row->start ) + ( ( date( "i", $row->start ) * (10/6) ) ) / 100 ) . "; ";
				echo "entry.end = " . ( date( "G", $row->end ) + ( ( date( "i", $row->end ) * (10/6) ) ) / 100 ) . "; ";
				echo "entry.title = '" . $row->title . "'; ";
				
			    echo "myDayArray.push( entry );";
			}
			?>

			var hourHeight = 25;
			var dayWidth = 400;

			var myNerdyCalendar = new nCal();
			
			window.addEvent('domready', function(){
				var container = $('day');
				var entries = $$('div.entry');
				
				myNerdyCalendar.setup( container, myDayArray );
				myNerdyCalendar.entriesOrganize();
				myNerdyCalendar.entriesUpdate();
			});
			
			function addEntry(event){
				var eventStart = ((event.client.y / (hourHeight / 2)).round()) * (hourHeight / 2);
				var url = "<?=BASE_URL(); ?>index.php/application/addEntry";
				
				new Ajax(url, {
					postBody: 'start=' + eventStart,
					onComplete: function(request){
						drawEntry(request)
					}
				}).request();
			}
			
			function drawEntry(request){
				var requestdata = request.split(',');
				myNerdyCalendar.entryCreate(requestdata[0], dayWidth, requestdata[1], requestdata[2]);
			}
			
			function deleteEntry(targetId){
				new Ajax('<?=BASE_URL(); ?>index.php/application/deleteEntry', {
					postBody: 'id=' + targetId
				}).request();
				myNerdyCalendar.entryDelete(targetId);
			}
		</script>
	</head>
	<body>
<?php
		$hourHeight = 25;
		$dayWidth = 400;
?>
		<div id="day" style="height: <?php echo 24 * $hourHeight; ?>px; width: <?php echo $dayWidth; ?>">
			<table height="<?php echo 24 * $hourHeight; ?>" width="<?php echo $dayWidth; ?>">
<?php
				for ($i = 0; $i < 24; $i++) {
					echo '<tr><td>&nbsp;</td></tr>';
				}
?>
			</table>
		</div>
		
		<script type="text/javascript" charset="utf-8">
			$('day').addEvent('dblclick', addEntry.bindWithEvent('day'));
		</script>
	</body>
</html>
